#include "spiRam.h"

#ifdef SPIRAM_LOCK

void *spiRam::lock = nullptr;
#endif

int spiRam::begin(QUAD_SPI *dev, uint32_t _begin, uint32_t _size) {
    sf = dev;
    start = _begin;
    size = _size;
#ifdef SPIRAM_LOCK

    if (nullptr==lock) {
        lock = Rtos::Create_Mutex();
    }
#endif
    return 0;
}

int spiRam::read(uint32_t addr, u8 *buf, uint32_t _size) {
    if ((addr + _size) > (start + size)) {
        logger.error("spiram read address out of range 0x%x not in 0x%x - 0x%x", addr, start, start + size);
        return -1;
    }
    int r = sf->readRamBytes(addr + start, buf, _size);
    return r == 0 ? _size : -2;
}

int spiRam::write(uint32_t addr, u8 *buf, uint32_t _size) {
    if ((addr + _size) > (start + size)) {
        logger.error("spiram write address out of range 0x%x not in 0x%x - 0x%x", addr, start, start + size);
        return -1;
    }
    int r = sf->writeRamBytes(addr + start, buf, _size);
    return r == 0 ? _size : -2;
}



